/* 
 * File: qtimetracker.h
 * Project: QTimeTracker
 * Author: Steve Sindelar, http://stevesindelar.cz
 * Licence: NewBSD licence.
 */

#ifndef QTIMETRACKER_H
#define QTIMETRACKER_H

#include <QSystemTrayIcon>
#include <QIcon>
#include <QtGui/QMainWindow>
#include <QString>
#include <QAbstractProxyModel >
#include <QTimer>
#include <QApplication>
#include <QObject>
#include "ui_qtimetracker.h"
#include "itemstablemodel.h"
#include "timespanstablemodel.h"

using namespace Core;

/** 
 * Contains GUI related classes.
 */
namespace Ui
{

	/**
	 * Main window of QTimeTracker application.
	 */
	class QTimeTracker : public QMainWindow
	{   
		Q_OBJECT

	public:
		QTimeTracker(QApplication& application);
		~QTimeTracker();    

		/**
		 * Overriden: for changingin tray menu.
		 */
		void setVisible(bool visible);
		/**
		 * Overriden: for handling system tray stuffs.
		 */
		void closeEvent(QCloseEvent *e);
		/**
		 * Indicates whether the constructor has successfuly initialized the object.
		 * E.g. error may occurre during loading icon images.
		 */
		bool isValid() { return this->_isValid; }

	protected slots:
		/** 
		 * @name Signals for menu and button actions.
		 * These signals are connected to corresponding menu action or button.
		 */
		//@{
		void showDetail();
		void openFile();
		void saveFileAs();
		void exportToCsv();
		void saveFile();
		void startItem();
		void createNewItem();
		void deleteItem();
		void deleteSpan();
		void showAboutDialog();    
		void stopAllItems();
		//@}

		/**
		 * For changing the behaviour of close event: user is asked to save his new data.
		 */
		void dataEdited() { this->saved = false; }
		/**
		 * Start button contains text 'Start' or 'Stop' according to state of selected item.
		 */
		void updateStartButtonText();
		/**
		 * Alert the user when data were not saved or some \ref Core::Item "Item" is 'running'.
		 */
		void onClose();
		/**
		 * Hides or releases the window to/from system tray when user clicks the sys tray icon.
		 */
		void trayIconActivated(QSystemTrayIcon::ActivationReason reason);
		/**
		 * Tooltip of the sys tray icon displays the information about how many items are running.
		 */
		void updateTrayToolTip();
		/**
		 * When timer ticks the displayed length of running items should be refreshed.
		 */
		void timerTimeout();

	private:    
		/** 
		 * @name Methods for initialization
		 * These methods are called only in constructor.
		 */
		//@{
		void initEditorFactory();
		void initMainTable();
		void initDetailTable();
		void initMenu();
		void connectButtonsSignals();
		void initTimer();
		bool initTray();
		//@}

		/**
		 * If this->trayInfoShown is false than shows tray icon information for user.
		 */
		void showTrayInfo();	

		/**
		 * Helper method for retrieving current index thru proxy model.
		 */
		QModelIndex getItemsCurrentIndex();
		/**
		 * Helper method for retrieving current index thru proxy model.
		 */
		QModelIndex getSpansCurrentIndex();

		/**
		 * Class generated by Qt Designer.
		 */
		Ui::QTimeTrackerClass ui;
		QApplication* application;
		bool _isValid;

		/**
		 * @name System tray stuff.
		 */
		//@{
		QSystemTrayIcon* trayIcon;
		QAction* restoreMinimizeAction;
		QIcon* runningIcon;
		QIcon* stopIcon;
		bool trayInfoShown;
		//@}

		/**
		 * @name For Save and SaveAs actions.
		 */
		//@{
		QString currentFileName;
		bool saved;
		//@}

		/** 
		 * List of running items, used for UI updating.
		 */
		QList<Item*> runningItems;

		/**
		 * Items model, this object is owner of items collection.
		 */
		ItemsTableModel* itemsModel;
		QAbstractProxyModel* itemsProxyModel;

		/**
		 * Model of spans of current (selected) item.
		 */ 
		TimeSpansTableModel* spansModel;
		QAbstractProxyModel* spansProxyModel;

		/**
		 * Timer for updating UI.
		 */
		QTimer* timer;    
	};

} //namespace

#endif // QTIMETRACKER_H
